package io.piper.datasource;

import com.alibaba.fastjson.JSON;
import io.piper.server.spring.pojo.entity.User;
import io.piper.server.spring.pojo.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class DataSourceService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private DataSourceService dataSourceService;

    @Config.Routing(Config.DataSourceEnum.SLAVE)
    public String find() {
        Config.DataSourceTypeManager.set(Config.DataSourceEnum.SLAVE);
        System.out.println(JSON.toJSONString(userMapper.selectByExample(null), true));
        System.out.println();
        dataSourceService.insert();
        String s = JSON.toJSONString(userMapper.selectByExample(null), true);
        System.out.println(s);
        return s;
    }

    @Config.Routing(Config.DataSourceEnum.MASTER)
    public void insert() {
        User user = new User();
        user.setId(0L);
        user.setNickname("newPiper");
        userMapper.insertSelective(user);
    }
}
